package cn.mybatisplus;

import cn.mybatisplus.entity.User;
import cn.mybatisplus.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest
public class MybatisPlusWrapperTest_ {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelectList(){
        /**
         *   SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
         *   WHERE is_deleted=0
         *   AND (user_name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
         */
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name","a")
                .between("age",20,30)
                .isNotNull("email");

        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

    @Test
    public void testSelectListSort(){
        /**
         *  SELECT uid AS id,user_name AS name,age,email,is_deleted
         *  FROM t_user
         *  WHERE is_deleted=0 ORDER BY age DESC,uid ASC
         */
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("age").orderByAsc("uid");
        List<User> users = userMapper.selectList(queryWrapper);

        users.forEach(System.out::println);

    }

    @Test
    public void testDeleteWrapper(){
        //UPDATE t_user SET is_deleted=1 WHERE is_deleted=0 AND (email IS NULL)
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("email");
        int delete = userMapper.delete(queryWrapper);

        System.out.println(delete);

    }

    @Test
    public void testUpdateWrapper(){

        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.gt("age",20)
                .like("user_name","a")
                .or()
                .isNull("email");

        User user = new User();

        user.setName("hahaha");
        user.setEmail("hahaha@qq.com");
        int update = userMapper.update(user, updateWrapper);

        System.out.println(update);

    }

    @Test
    public void testUpdateRank(){

        /**
         *  UPDATE t_user SET user_name=?, email=?
         *  WHERE is_deleted=0 AND (user_name LIKE ?
         *  AND (age > ? OR email IS NULL))
         */
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name","h")
                .and(i->i.gt("age",20).or().isNull("email"));

        User user = new User();

        user.setName("kkkkkkkkk");
        user.setEmail("kkkkkkkkk@qq.com");

        int update = userMapper.update(user, queryWrapper);

        System.out.println(update);

    }

    @Test
    public void testSelectColumn(){
        // SELECT user_name,email FROM t_user WHERE is_deleted=0
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_name","email");
        List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
        maps.forEach(System.out::println);
    }

    @Test
    public void testSelectSon(){
        /**
         * SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
         * WHERE is_deleted=0
         * AND (uid IN (select uid from t_user where uid <=100))
         */
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("uid","select uid from t_user where uid <=100");

        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);

    }

    @Test
    public void testUpdateWrapper2(){
        /**
         *  UPDATE t_user SET user_name=?,email=?
         *   WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
         */
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.like("user_name","a")
                .and(i -> i.gt("age",20).or().isNull("email"));

        updateWrapper.set("user_name","小黑").set("email","abc@163.com");
        int update = userMapper.update(null, updateWrapper);

        System.out.println(update);
    }

    @Test
    public void test(){
        /**
         *  SELECT uid AS id,user_name AS name,age,email,is_deleted
         *   FROM t_user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
         */
        String username = "";
        Integer ageBegin = 20;
        Integer ageEnd = 30;

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // StringUtils.isNotBlank(username)判断某个字符串是否不为空 不为null,不为空白符
        if (StringUtils.isNotBlank(username)){
            queryWrapper.like("username",username);
        }
        if (ageBegin != null){
            queryWrapper.ge("age",ageBegin);
        }

        if (ageBegin != null){
            queryWrapper.le("age",ageEnd);
        }

        List<User> rlist = userMapper.selectList(queryWrapper);

        rlist.forEach(System.out::println);

    }

    @Test
    public void testCondition(){
        /**
         *  SELECT uid AS id,user_name AS name,age,email,is_deleted
         *  FROM t_user
         *  WHERE is_deleted=0 AND (age < ?)
         */
        String username = "";
        Integer ageBegin = null;
        Integer ageEnd = 30;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(username),"user_name",username)
                .ge(ageBegin != null,"age",ageBegin)
                .lt(ageEnd != null,"age",ageEnd);

        List<User> rlist = userMapper.selectList(queryWrapper);

        rlist.forEach(System.out::println);
    }

    @Test
    public void testLamdaQueryWrapper(){
        String username = "h";
        Integer ageBegin = null;
        Integer ageEnd = 30;

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(username), User::getName,username)
                .ge(ageBegin != null,User::getAge,ageBegin)
                .lt(ageEnd != null,User::getAge,ageEnd);

        List<User> rlist = userMapper.selectList(queryWrapper);
        rlist.forEach(System.out::println);
    }

    @Test
    public void testLamdaUpdateWrapper(){
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.like(User::getName,"黑")
                .and(i -> i.gt(User::getAge,20).or().isNull(User::getEmail));

        updateWrapper.set(User::getName,"小嗨").set(User::getAge,"abc@163.com")
                .set(User::getAge,10);
        int update = userMapper.update(null, updateWrapper);

        System.out.println(update);
    }
}
